<?php
/**
* Author: Porlock
* Link: www.porlockz.com
* Date: 2018-04-09 15:30:50
* Last Modified time: 
* Use：展示所有队伍排名及本队伍排名,每一页显示二十名选手，根据页数进行切换
*/
require_once ('includes/lib/func_rightCtrl.php');
// 权限控制
player_rightCtrl();
$uid = $_SESSION['uid'];
// 换页部分处理
$playerNum_query = "SELECT `uid` FROM `user`";
$sth_playernum   = $dbh -> prepare($playerNum_query);
$sth_playernum	 ->execute();
$playernum 	     = $sth_playernum->rowCount();
$page = @$_GET['page'];
if (!isset($page)){
	$page = 1;
}
// 首页
$first_page = 1;
// 上一页
$last_page = $page -1;
// 下一页
$next_page = $page + 1;
// 尾页
$trailer_page = (int)($playernum / 20) + 2;
// 如果上一页超出了首页，设为首页
if($page <= $first_page){
	$page = $first_page;
	$last_page = $first_page;
}
// 如果下一页超出尾页，设为尾页
elseif($page >= $trailer_page){
	$page = $trailer_page;
	$next_page = $trailer_page;
}
// 配合查询语句处理后的当前页[limit从0开始]
$page = $page - 1;
// 传入查询语句的页面换算值
$pageValue = $page * 20;
// 1.获取当前用户数据
$currentPlayer_query = "SELECT `nickname`,`skilled`,`last_time`,`totalpoint`,`rank` FROM `user` WHERE `uid` = :uid";
$sth_currentPlayer = $dbh -> prepare($currentPlayer_query);
$sth_currentPlayer -> bindParam(":uid",$uid,PDO::PARAM_STR);
$sth_currentPlayer -> execute();
$currentPlayer_result  = $sth_currentPlayer -> fetch();
$currentPlayer_nickname   = $currentPlayer_result['nickname'];
$currentPlayer_skilled    = $currentPlayer_result['skilled'];
$currentPlayer_last_time  = $currentPlayer_result['last_time'];
$currentPlayer_totalpoint = $currentPlayer_result['totalpoint'];
$currentPlayer_rank       = $currentPlayer_result['rank'];
echo <<<EOT
<link href="/afctf/css/rank.css" rel="stylesheet" type="text/css" media="all"/>
<div class="challenge_table">
	<table border="1" class="layui-table">
		<blockquote class="layui-elem-quote">你的排名</blockquote>
		<thead>
		  <tr align="center">
		    <th>昵称</th>
		    <th>个人强项</th>
		    <th>最近一次得分时间</th>
		    <th>总分数</th>
		    <th>排行</th>
		  </tr>
		</thead>
		<tbody>
		<tr align="center">
	    <td>$currentPlayer_nickname</td>
	    <td>$currentPlayer_skilled</td>
	    <td>$currentPlayer_last_time</td>
	    <td>$currentPlayer_totalpoint</td>
	    <td>$currentPlayer_rank</td>
	  </tr>
		</tbody>
	</table>
EOT;
// 2.获取所有用户排行
$player_query = "SELECT `nickname`,`skilled`,`last_time`,`totalpoint`,`rank` FROM `user` ORDER BY `totalpoint` DESC,`rank` ASC limit :page,20";
$sth_player = $dbh -> prepare($player_query);
$sth_player -> bindParam(":page",$pageValue,PDO::PARAM_INT);
$sth_player -> execute();
$rownum = $sth_player->rowCount();
echo <<<EOT
	<table border="1" class="layui-table">
		<blockquote class="layui-elem-quote">排行榜</blockquote>
		<thead>
		  <tr align="center">
		    <th>昵称</th>
		    <th>个人强项</th>
		    <th>最近一次得分时间</th>
		    <th>总分数</th>
		    <th>排行</th>
		  </tr>
		</thead>
		<tbody>
EOT;
if($rownum === 0){
	echo <<<EOT
	<tr align="center">
	    <td colspan="5">暂无数据</td>
	</tr>
EOT;
}
else{
	for($i = 0;$i < $rownum; $i++){
		$player_result     = $sth_player -> fetch();
		$player_nickname   = $player_result['nickname'];
		$player_skilled    = $player_result['skilled'];
		$player_last_time  = $player_result['last_time'];
		$player_totalpoint = $player_result['totalpoint'];
		$player_rank       = $player_result['rank'];
    	echo <<<EOT
		<tr align="center">
	    <td>$player_nickname</td>
	    <td>$player_skilled</td>
	    <td>$player_last_time</td>
	    <td>$player_totalpoint</td>
	    <td>$player_rank</td>
	  </tr>
EOT;
}
}
echo <<< EOT
		</tbody>
			</table>
		<div id="page">	
			<button class="layui-btn" onclick="jump($first_page)">
				首页
			</button>
			<button class="layui-btn" onclick="jump($last_page)">
				上一页
			</button>
			<button class="layui-btn" onclick="jump($next_page)">
				下一页
			</button>
			<button class="layui-btn" onclick="jump($trailer_page)">
				尾页
			</button>
			<input type="text" placeholder="页号" id="SetPage" size="5" maxlength="2" style="margin-left:10px"></input>
			<button onclick="skip()" class="layui-btn skip" style="margin-left:10px">
				 	跳转
			</button>
		</div>	
		
</div>
<script type="text/javascript">
    function jump(page){
        self.location.href="?action=rank&page=" + page;
    }
    function skip(){
    	var regPos = /^\d+$/;
    	if(SetPage.value == ""){
    		alert("请输入页号!");
    	}
    	else if(!regPos.test(SetPage.value)){
			self.location.href="?action=rank&page=" + $first_page;
    	}
    	else if(SetPage.value < $first_page || SetPage.value > $trailer_page){
    		self.location.href="?action=rank&page=" + $first_page;
    	}
    	else{
    		self.location.href="?action=rank&page=" + SetPage.value;
    	}
    }
</script>

EOT;

?>